<?php
/**
 * @author 罗文鸿
 * @date 2024/5/27
 */

// 获取参数
$searchKeyword = isset($_GET['search']) ? $_GET['search'] : '';
$searchType = isset($_GET['searchType']) ? $_GET['searchType'] : 'username';
include '../config/Config.php';
// 查询数据
// 按名字查询sql
$querySql = '';
if ($searchType == 'username') {
    $querySql = " SELECT id, username, gender ,phone ,add_time addTime FROM user WHERE  username LIKE '$searchKeyword%'   AND del = 0 ORDER BY addTime DESC LIMIT 10 ";
} else {
    // 按手机号查询sql
    $querySql = "SELECT id, username, gender ,phone ,add_time addTime FROM user WHERE  phone LIKE '$searchKeyword%'  AND del = 0  ORDER BY addTime DESC LIMIT 10 ";
}
// 获取查询结果
$conn = getDatabaseConnection();

$queryRes = $conn->query($querySql);


// 检查查询结果
if (count($queryRes) > 0) {
    // 输出数据
    foreach ($queryRes as $row) {
        echo "<tr>";
        echo "<td>" . $row["id"] . "</td>";
        echo "<td>" . $row["username"] . "</td>";
        echo "<td>" . $row["phone"] . "</td>";

        // 根据性别枚举值输出对应的文本
        switch ($row["gender"]) {
            case 0:
                echo "<td>女</td>";
                break;
            case 1:
                echo "<td>男</td>";
                break;
            default:
                echo "<td>未知</td>";
        }

        echo "<td>" . date('Y-m-d H:i:s', ($row["addTime"])) . "</td>";
        echo "<td><button onclick='deleteUser(" . $row["id"] . ")'>删除</button></td>";
        echo "</tr>";
    }
} else {
    echo "<tr><td colspan='3'>0 结果</td></tr>";
}

// 关闭连接
$conn->close();

// 展示查询结果
echo "<h1>搜索结果</h1>";
echo "<p>您搜索的关键词是：" . htmlspecialchars($searchKeyword) . "</p>";

